package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CusProject;
import com.ruoyi.system.service.ICusProjectService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目管理
 * 
 * @author ruoyi
 * @date 2022-01-10
 */
@Controller
@RequestMapping("/system/project")
public class CusProjectController extends BaseController
{
    private String prefix = "system/project";
    
	@Autowired
	ISysUserService sysUserService;

    @Autowired
    private ICusProjectService cusProjectService;

    @RequiresPermissions("system:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("system:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CusProject cusProject)
    {
        startPage();
        List<CusProject> list = cusProjectService.selectCusProjectList(cusProject);
        return getDataTable(list);
    }
    
    
    /**
     * 下拉列表（select2）
     */
    // @RequiresPermissions("system:user:list")
    @RequestMapping("/select2")
    @ResponseBody
    public Object select2(CusProject project, String q)
    {
        // data-ajax--data="projectStatus=0"
    	project.setProjectStatus("0");
    	project.setProjectName(q);
    	project.setProjectCode(q);
        		
    	List<CusProject> list = cusProjectService.selectCusProjectList(project);
		return getSelect2(list, new Function<CusProject, HashMap<String,String>>() {
			@Override
			public HashMap<String, String> apply(CusProject t) {
				HashMap<String, String> data = new HashMap<>();
				data.put("id", t.getID() + "");
				data.put("text", String.format("%s (%s)", t.getProjectCode(), t.getProjectName()));
				return data;
			}
		});
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("system:project:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CusProject cusProject)
    {
        List<CusProject> list = cusProjectService.selectCusProjectList(cusProject);
        ExcelUtil<CusProject> util = new ExcelUtil<CusProject>(CusProject.class);
        return util.exportExcel(list, "项目数据");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("system:project:add")
    @Log(title = "添加项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CusProject cusProject)
    {    	
        return toAjax(cusProjectService.insertCusProject(cusProject));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        CusProject cusProject = cusProjectService.selectCusProjectByID(ID);
        mmap.put("cusProject", cusProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("system:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CusProject cusProject)
    {
    	System.out.println(cusProject.getProjectStart());
        return toAjax(cusProjectService.updateCusProject(cusProject));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("system:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cusProjectService.deleteCusProjectByIDs(ids));
    }
}
